//
//  HomeViewController.m
//  BaseProject
//
//  Created by hundredsandc on 2017/10/25.
//  Copyright © 2017年 hundredsandc. All rights reserved.
//

#import "HomeViewController.h"
#import "SDCycleScrollView.h"
#import "Masonry.h"
#import "HTTPManagers.h"

#import "HomeCustomBtn.h"
#import "HomeCollectionViewCell.h"
#import "HomeCollectionReusableView.h"
#import "TouTiaoVC.h"
#import "JiJinViewController.h"

#import "WEBViewController.h"

@interface HomeViewController ()<SDCycleScrollViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic,strong)SDCycleScrollView *headerCycleView;
@property (nonatomic,strong)UIView *midContendView;
@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,strong)UICollectionViewFlowLayout *flowLayOut;
@property (nonatomic,strong)NSMutableArray *dataArr;
@property (nonatomic,strong)NSDictionary *dict;
@property (nonatomic,strong)NSMutableArray *imageUrlArr;

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getData];
//    [self getVideoData];
    [self setupUI];
    [self addConstraint];
}
#pragma mark - UI
-(void)setupUI{
    [self addHeaderCycleView];
    [self addMidFunctionView];
    [self.view addSubview:self.collectionView];
  
}
#pragma mark -Action
-(void)getData{
    NSString *timeSp = [self getNowTimeTimestamp3];
//    http://a8.tvesou.com/v2/hot/main?leagueId=0&version=204&time=1502029079&sign=5ec702a949b361773b5c662a461874bd&channel=AppStore&os_ver=10.2.1&platform=ios
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"leagueId"]=@"0";
    params[@"version"] = @(204);
    params[@"time"] = timeSp;
    params[@"sign"]= @"5ec702a949b361773b5c662a461874bd";
    params[@"channel"] = @"AppStore";
    params[@"os_ver"] = @"10.2.1";
    params[@"platform"] = @"ios";
    
    
    [HTTPManagers getWithHomeURL:@"main" params:params success:^(id json) {
        
        [_collectionView.mj_header endRefreshing];
        self.dict = json[@"data"];
        [self reloadData];
        
        
        
    } failure:^(NSError *error) {
        
    }];


}
-(void)reloadData{
    NSArray *banner = self.dict[@"banner"];
    if (self.imageUrlArr.count>0) {
        [self.imageUrlArr removeAllObjects];
    }
    for (NSDictionary *dict in banner) {
        NSString *imageUrl  = dict[@"img"];
        [self.imageUrlArr addObject:imageUrl];
    }
    
    NSArray *imageArr = @[@"banner1",@"banner2-",@"banner3"];
    _headerCycleView.localizationImageNamesGroup = imageArr;
//    _headerCycleView.imageURLStringsGroup = self.imageUrlArr;
    
    
    [_collectionView reloadData];
}
-(void)addHeaderCycleView{
    _headerCycleView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectZero delegate:self placeholderImage:nil];
    _headerCycleView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
    _headerCycleView.pageDotColor = [UIColor lightGrayColor];
    _headerCycleView.autoScrollTimeInterval = 2.0;
    
    [self.view addSubview:_headerCycleView];
}

-(void)addMidFunctionView{
    _midContendView = [[UIView alloc]init];
    
    HomeCustomBtn *btn1 = [[HomeCustomBtn alloc]initWithFrame:CGRectMake(15, 0, (SCREEN_WIDTH-15-0.50-15)/2, 69.5) title:@"头条" subTitle:@"给您最新鲜" imageUrlStr:@"toutiao" isLeft:YES];
    HomeCustomBtn *btn2 = [[HomeCustomBtn alloc]initWithFrame:CGRectMake(15+btn1.width+0.5, 0,btn1.width, 69.5) title:@"NBA" subTitle:@"天生为球狂" imageUrlStr:@"nba" isLeft:NO];
    HomeCustomBtn *btn3 = [[HomeCustomBtn alloc]initWithFrame:CGRectMake(15,70, (SCREEN_WIDTH-15-0.50-15)/2, 69.5) title:@"集锦" subTitle:@"给您最热血" imageUrlStr:@"jijing" isLeft:YES];
    HomeCustomBtn *btn4 = [[HomeCustomBtn alloc]initWithFrame:CGRectMake(15+btn1.width+0.5,70,btn1.width, 69.5) title:@"足球" subTitle:@"刺激您的心脏" imageUrlStr:@"zucai" isLeft:NO];
    
    btn1.tag = 1000+1;
    btn2.tag = 1000+2;
    btn3.tag = 1000+3;
    btn4.tag = 1000+4;
    
    [btn1 addTarget:self action:@selector(MidBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [btn2 addTarget:self action:@selector(MidBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [btn3 addTarget:self action:@selector(MidBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [btn4 addTarget:self action:@selector(MidBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [_midContendView addSubview:btn1];
    [_midContendView addSubview:btn2];
    [_midContendView addSubview:btn3];
    [_midContendView addSubview:btn4];
    
    
    [self.view addSubview:_midContendView];
}
#pragma mark - collection
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    NSArray *videos =self.dict[@"video"];
    return videos.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HomeCollectionViewCell *item = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([HomeCollectionViewCell class]) forIndexPath:indexPath];
    NSArray *video  = self.dict[@"video"];
    if (video.count>0) {
        
        NSDictionary *dict = video[indexPath.item];
        NSInteger timeSp = [dict[@"pubTime"]longLongValue];
        NSString*time = [YXComUtil changeTimeToTimeStr:timeSp formate:@"yyyy.MM.dd"];
        NSDictionary *videoDict = [dict[@"videos"]firstObject];
        
        [item customImageUrl:videoDict[@"thumb"] time:time title:dict[@"title"] type:dict[@"leagueName"]];
        
    }

    return item;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((SCREEN_WIDTH-39)/2, 137);
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    HomeCollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([HomeCollectionReusableView class]) forIndexPath:indexPath];
    reusableView.titleLabel.text = @"热门（足球视屏）";
    reusableView.subTitleLabel.text = @"更多";
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(moreVideoClick:)];
    [reusableView addGestureRecognizer:tap];
    return reusableView;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(SCREEN_WIDTH, 41);
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(10, 15, 0, 15);
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"11");
    NSArray *video  = self.dict[@"video"];
    NSDictionary *dict = video[indexPath.item];
    NSDictionary *videoDict = [dict[@"videos"]firstObject];
    WEBViewController *web = [WEBViewController new];
    web.url = videoDict[@"url"];
    web.titleStr = dict[@"title"];
    [self.navigationController pushViewController:web animated:YES];
}
#pragma mark - constraint
-(void)addConstraint{
    WS(ws);
    [_headerCycleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(ws.view);
        make.height.mas_equalTo(150);
        make.width.mas_equalTo(SCREEN_WIDTH);
    }];
    
    [_midContendView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_headerCycleView.mas_bottom).with.offset(10);
        make.width.mas_equalTo(SCREEN_WIDTH);
        make.height.mas_equalTo(140);
        make.left.equalTo(ws.view);
    }];
    
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_midContendView.mas_bottom);
        make.bottom.mas_equalTo(ws.view).with.offset(-64);
        make.width.mas_equalTo(SCREEN_WIDTH);
        make.left.equalTo(ws.view);
    }];

}
#pragma mark - Action
-(void)moreVideoClick:(UITapGestureRecognizer *)tap{
    NSLog(@"更多视频");
    JiJinViewController *jijin = [JiJinViewController new];
    jijin.isMoreVideo = YES;
    [self.navigationController pushViewController:jijin animated:YES];
}
-(void)MidBtnClick:(UIButton*)sender{
    if (sender.tag==1001) {
        TouTiaoVC *toutiao = [TouTiaoVC new];
        toutiao.index = sender.tag;
        [self.navigationController pushViewController:toutiao animated:YES];
        NSLog(@"头条");
    }else if (sender.tag == 1002){
        NSLog(@"NBA");
        TouTiaoVC *toutiao = [TouTiaoVC new];
        toutiao.index = sender.tag;
        [self.navigationController pushViewController:toutiao animated:YES];
    }else if (sender.tag == 1003){
        JiJinViewController *jijin = [JiJinViewController new];
        [self.navigationController pushViewController:jijin animated:YES];
        NSLog(@"集锦");
    }else if (sender.tag == 1004){
        NSLog(@"足球");
        TouTiaoVC *toutiao = [TouTiaoVC new];
        toutiao.index = sender.tag;
        [self.navigationController pushViewController:toutiao animated:YES];
    }
}
-(UICollectionView *)collectionView{
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:self.flowLayOut];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([HomeCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([HomeCollectionViewCell class])];
        [_collectionView registerClass:([HomeCollectionReusableView class]) forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([HomeCollectionReusableView class])];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self getData];
            
        }];
    }
    return _collectionView;
}
-(UICollectionViewFlowLayout *)flowLayOut{
    if (!_flowLayOut) {
        _flowLayOut = [[UICollectionViewFlowLayout alloc] init];
        
        _flowLayOut.minimumInteritemSpacing = 9;
        _flowLayOut.minimumLineSpacing = 15;
        [_flowLayOut setScrollDirection:UICollectionViewScrollDirectionVertical];
        
    }
    return _flowLayOut;
}
-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}
-(NSMutableArray *)imageUrlArr{
    if (!_imageUrlArr) {
   _imageUrlArr = [NSMutableArray array];
    }
    return _imageUrlArr;
}
-(NSDictionary *)dict{
    if (!_dict) {
        _dict = [NSDictionary dictionary];
    }
    return _dict;
}
@end
